var app = getApp();
Page({
  data: {
    couseInforList: null,
    conmmentList: null,

    courseId: null,
    cChooseID: null,
    time: null,
    cRemark: "5.0",
    cAddress: null,
    tName: null,
    cName: null,
    date: null,
    student: null,
    courseMark: null,  //评分
    commentPeople: null,  //评论人数

    cId: null,
    teachername: "未知"

  },
  onLoad: function (options) {
    var that = this
    console.log(options)
    var _courseID = options.courseId;
    var _cChooseID = options.cChooseID;
    var _time = options.time;
    that.setData({
      courseId: _courseID,
      cChooseID: _cChooseID,
      time: _time,
      student: app.globalData.sId
    })
    console.log(_cChooseID);
    if (_cChooseID != null) {
      getCouseListInfo(that);
      getCouseMark(that);
    }

  },

  onReady: function () {

  },

  onShow: function () {

  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  },
})

function getCouseMark(that) {
  //得到课程的学生评分以及评论的总人数
  wx.request({
    url: 'https://geek-team.xin/evaluation/getSum',
    data: {
      course: that.data.cChooseID,
      time: that.data.time
    },
    success(e) {
      console.log(e)
      var _num = parseInt(e.data.average)
      _num = _num.toFixed(1)
      that.setData({
        commentPeople: e.data.sum,
        cRemark: _num
      })
    },
  })

}

function getCouseListInfo(that) {
  wx.request({
    url: 'https://geek-team.xin/course/findById',
    data: {
      Id: that.data.cChooseID,
    },
    success(e) {
      console.log(e)
      that.setData({
        couseInforList: e.data,
        cId: e.data.cId
      })
      getTeacher(that);
    },
  })

}

function getTeacher(that) {
  console.log(that.data.cId)
  wx.request({
    url: 'https://geek-team.xin/teacher/findById',
    data: {
      tId: that.data.cId,
    },
    success(e) {
      console.log(e)
      that.setData({
        teachername: e.data.tName
      })
    },
  })

}